package mx.kolobok.noteeditor.ui.keymap.actions;

import javax.swing.*;
import javax.swing.text.DefaultEditorKit;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.FlavorEvent;
import java.awt.datatransfer.FlavorListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

/**
 * User:  Nikita Belenkiy
 * Date: 20.05.11
 * Time: 4:24
 */
public class PasteAction extends DefaultEditorKit.PasteAction {

    public PasteAction() {
        putValue(Action.NAME, "Paste");
               putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK));
        Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        systemClipboard.addFlavorListener(new FlavorListener() {
            @Override
            public void flavorsChanged(FlavorEvent e) {
                Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                boolean dataFlavorAvailable = systemClipboard.isDataFlavorAvailable(DataFlavor.stringFlavor);
                setEnabled(dataFlavorAvailable);
            }
        });
    }
}
